<?php
/** 
 * 权限规则模型基类
 *
 * @author   xionggan
 * @date     2019-09-17
 */

namespace app\common\model;

use think\Db;
use think\Model;

class AuthRule extends Model
{
    protected $name  = 'auth_rule';
    protected $error = null;
    protected $append = [
        'status_text',
        'is_menu_text',
        'is_node_text',
    ];

    const STATUS_0  = 0;
    const STATUS_1  = 1;

    const IS_MENU_0  = 0;
    const IS_MENU_1  = 1;

    const IS_NODE_0  = 0;
    const IS_NODE_1  = 1;
 
    /**
     * 获取状态映射
     * @return  array   状态映射表
     */
    public function getStatusMap()
    {
        return [
            self::STATUS_0  => '禁用',
            self::STATUS_1  => '正常',
        ];
    }

    /**
     * 获取是否菜单映射表
     * @return  array   状态映射表
     */
    public function getIsMenuMap()
    {
        return [
            self::IS_MENU_0  => '否',
            self::IS_MENU_1  => '是',
        ];
    }

    /**
     * 获取是否节点映射表
     * @return  array   状态映射表
     */
    public function getIsNodeMap()
    {
        return [
            self::IS_NODE_0  => '否',
            self::IS_NODE_1  => '是',
        ];
    }

    public function getStatusTextAttr($value, $data)
    {
        $map = $this->getStatusMap();
        return $map[$data['status']] ?? '';
    }

    public function getIsMenuTextAttr($value, $data)
    {
        $map = $this->getIsMenuMap();
        return $map[$data['is_menu']] ?? '';
    }

    public function getIsNodeTextAttr($value, $data)
    {
        $map = $this->getIsNodeMap();
        return $map[$data['is_node']] ?? '';
    }

    public function getCreateTimeAttr($value, $data)
    {
        return empty($data['create_time']) ? '' : date('Y-m-d H:i:s', $data['create_time']);
    }
    
}
